using System;
using System.Text;
using Sinacor.OMM.Translator.Interfaces;

namespace Sinacor.OMM.Translator.Translators.STM
{
    public class MessageAN : BaseTranslator, ITranslator
    {
        private static MessageAN _instance = null;

        private MessageAN()
        {
        }

        public static MessageAN GetInstance()
        {
            if (_instance == null)
                _instance = new MessageAN();

            return _instance;
        }

        #region ITranslator Members

        public Sinacor.OMM.MessageEntity.BaseMessage TranslateToRouterMessage()
        {
            return TranslateToMessage();
        }

        public Sinacor.OMM.MessageEntity.BaseMessage TranslateToProcessMessage()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Sinacor.OMM.MessageEntity.BaseMessage TranslateToMessage()
        {
            Sinacor.OMM.MessageEntity.STM.MessageAN _message = new Sinacor.OMM.MessageEntity.STM.MessageAN();
            MessageEntity.BaseMessage baseMessage;
            DateTime _date;
            int admMessageCode = 0;

            _message.CblcIdentificator = Sinacor.OMM.MessageEntity.Enum.MessagesCBLCSource.AN;
            _message.Source = Sinacor.OMM.MessageEntity.Enum.MessageSource.CBLC;
            _message.Original = OriginalMessage.Body;

            if (DateTime.TryParse(OriginalMessage.Body.Substring(6, 10), out _date))
            {
                _message.System = "ADM";
            }
            else
            {
                _message.System = "NEG";
            }

            if(Int32.TryParse(OriginalMessage.Body.Substring(
                LayoutCatalog.STM.MessageANPhaseControl.AdmMessageCode.StartIndex, LayoutCatalog.STM.MessageANPhaseControl.AdmMessageCode.Length), out admMessageCode))            
                _message.AdmMessageCode = admMessageCode;            

            baseMessage = TranslateMessageLabel(OriginalMessage.Label);
            _message.SequentialID = baseMessage.SequentialID;
            _message.ID = baseMessage.ID;
            _message.OperationDate = GetOperationDate(_message.ID);

            return _message;
        }
    }
}